if SERVER then
   AddCSLuaFile( "shared.lua" )
end

if CLIENT then
   SWEP.DrawCrosshair   = false -- Use Metastrike crosshair
   SWEP.ViewModelFOV    = 82
   SWEP.ViewModelFlip   = true
   SWEP.CSMuzzleFlashes = true
   
function SWEP:AdjustMouseSensitivity()
	local scoped = self:GetNWBool("ScopeUsing", false)
	if scoped then return 0.25 else return 1 end
end

function SWEP:DrawHUD()
	--[[
	if self:GetNWBool("ScopeUsing", false) then
		surface.SetTexture( surface.GetTextureID("VGUI/ms_scope") )
   		surface.SetDrawColor( 255, 255, 255, 255 )
   		surface.DrawTexturedRect( ScrW()/2-ScrH()/2, 0, ScrH(), ScrH() )
		surface.SetDrawColor( 0, 0, 0, 255 )
		surface.DrawRect( 0,0,(ScrW()-ScrH())/2,ScrH() )
		surface.DrawRect( ScrW()-(ScrW()-ScrH())/2,0,(ScrW()-ScrH())/2,ScrH() )
	end
	--]]--
end
end

SWEP.Base = "weapon_base"

SWEP.Category           = "Meta-Strike"
SWEP.Spawnable          = false
SWEP.AdminSpawnable     = false

SWEP.Weight             = 5
SWEP.AutoSwitchTo       = true
SWEP.AutoSwitchFrom     = true

SWEP.Primary.Sound          = Sound( "Weapon_Pistol.Empty" )
SWEP.Primary.CritSound		= Sound( "weapons/airboat/airboat_gun_lastshot1.wav" )
SWEP.Primary.Recoil         = 1.5
SWEP.Primary.Damage         = 1
SWEP.Primary.NumShots       = 1
SWEP.Primary.Cone           = 0.02
SWEP.Primary.Delay          = 0.15

SWEP.Primary.ClipSize       = -1
SWEP.Primary.DefaultClip    = -1
SWEP.Primary.Automatic      = false
SWEP.Primary.Ammo           = "none"
SWEP.Primary.ClipMax        = -1

SWEP.Secondary.ClipSize     = -1
SWEP.Secondary.DefaultClip  = -1
SWEP.Secondary.Automatic    = false
SWEP.Secondary.Ammo         = "none"
SWEP.Secondary.ClipMax      = -1

SWEP.PrimaryAnim = ACT_VM_PRIMARYATTACK
SWEP.ReloadAnim = ACT_VM_RELOAD

SWEP.HeadshotMultiplier = 2.7

SWEP.DeploySpeed = 0.5

-- Meta-Strike variables n shit.
SWEP.HasOwnScope = false
SWEP.HeatFactor = 1

-- Here's where the magic begins to happen.

function SWEP:Initialize()

	self:SetWeaponHoldType( self.HoldType )
	self:SetNWBool("ScopeUsing", false)
	self:SetNWBool("CriticalHit", false)
end

function SWEP:Deploy()
	self:SetNWBool("ScopeUsing", false)
	self:SetNWBool("CriticalHit", false)
	return true
end

function SWEP:Holster()
	self.Owner:SetFOV( 0, 0 )
	return true
end

function SWEP:PrimaryAttack(worldsnd)
	
	self.Weapon:SetNextSecondaryFire( CurTime() + 0.1 )

	if not self:CanPrimaryAttack() then return end
	self.Weapon:SetNextPrimaryFire( CurTime() + self:GetModDelay() )
	self:DetermineCriticalHit()
	if !GAMEMODE:MSCheckForMod("Silent Guns") then
		if not worldsnd then
			self.Weapon:EmitSound( self:GetModSound(), self.Primary.SoundLevel )
			if self:GetCriticalHit() then self.Weapon:EmitSound( self.Primary.CritSound, self.Primary.SoundLevel ) end
		elseif SERVER then
			sound.Play(self:GetModSound(), self:GetPos(), self.Primary.SoundLevel)
			if self:GetCriticalHit() then sound.Play(self.Primary.CritSound, self:GetPos(), self.Primary.SoundLevel) end
		end
	end

	self:ShootBullet( self:GetModDamage(), self.Primary.Recoil, self.Primary.NumShots, self:GetModCone() )

	self:TakePrimaryAmmo( 1 )

	local owner = self.Owner
	if not IsValid(owner) or owner:IsNPC() or (not owner.ViewPunch) then return end

	owner:ViewPunch( Angle( math.Rand(-0.2,-0.1) * self.Primary.Recoil, math.Rand(-0.1,0.1) *self.Primary.Recoil, 0 ) )
end

function SWEP:SecondaryAttack()
	if (SERVER) then
		if GAMEMODE:MSCheckForMod("All Guns Have Scopes") or self.HasOwnScope then
			if self:GetNWBool("ScopeUsing") then
				self:SetNWBool("ScopeUsing", false)
				self.Owner:SetFOV( 0, 0.25 )
			else
				self:SetNWBool("ScopeUsing", true)
				self.Owner:SetFOV( 20, 0.25 )
			end
		end
	end
end

function SWEP:Reload()
	if self:GetNWBool("ScopeUsing",false) then
	self:SetNWBool("ScopeUsing", false)
	self.Owner:SetFOV( 0, 0.2 )
	end
	self.Weapon:DefaultReload(self.ReloadAnim)
end

function SWEP:DryFire(setnext)
	self:EmitSound( "Weapon_Pistol.Empty" )

	setnext(self, CurTime() + 0.2)

	self:Reload()
end

function SWEP:CanPrimaryAttack()
	-- Ohoh no I am NOT doing Overheat JUST YET
	if not IsValid(self.Owner) then return false end
	if self.Owner:KeyDown(IN_SPEED) and !GAMEMODE:MSCheckForMod("Attack While Sprinting") then return false end
	if !self.Owner:KeyDown(IN_DUCK) and GAMEMODE:MSCheckForMod("Crouch to Shoot") then return false end
	if self.Weapon:Clip1() <= 0 then
		self:DryFire(self.SetNextPrimaryFire)
		return false
	end
	return true
end

function SWEP:ShootBullet( dmg, recoil, numbul, cone )

   self.Weapon:SendWeaponAnim( ACT_VM_PRIMARYATTACK )

   self.Owner:MuzzleFlash()
   self.Owner:SetAnimation( PLAYER_ATTACK1 )

   if not IsFirstTimePredicted() then return end

   --local sights = self:GetIronsights()

   numbul = numbul or 1
   cone   = cone   or 0.01
   missed = false
   if SERVER then
   if GAMEMODE:MSCheckForMod("50% Chance for No Damage") and math.Round(math.random())==1 then 
		missed = true 
		dmg = 0
   end
   end
   local bullet = {}
   bullet.Num    = numbul
   bullet.Src    = self.Owner:GetShootPos()
   bullet.Dir    = self.Owner:GetAimVector()
   bullet.Spread = Vector( cone, cone, 0 )
   bullet.Tracer = 1
   bullet.TracerName = self:GetModTracer()
   bullet.Force  = 10
   if GAMEMODE:MSCheckForMod("Knockback Bullets") then bullet.Force=600 end
   bullet.Damage = dmg
   bullet.Callback = function (attacker, tr, dmginfo) self:MetastrikeCallback(attacker, tr, dmginfo) end
        

   self.Owner:FireBullets( bullet )

   -- Owner can die after firebullets
   if (not IsValid(self.Owner)) or (not self.Owner:Alive()) or self.Owner:IsNPC() then return end

   if ((game.SinglePlayer() and SERVER) or
       ((not game.SinglePlayer()) and CLIENT and IsFirstTimePredicted())) then

      -- reduce recoil if ironsighting
      recoil = sights and (recoil * 0.6) or recoil

      local eyeang = self.Owner:EyeAngles()
      eyeang.pitch = eyeang.pitch - recoil
      self.Owner:SetEyeAngles( eyeang )
   end

end

-- Begin strike mod functions n shit
function SWEP:GetModDamage(dmg)
	local dmg = dmg or self.Primary.Damage
	if GAMEMODE:MSCheckForMod("Gun Damage Increased") then dmg = dmg * 2 end
	if GAMEMODE:MSCheckForMod("Gun Damage Decreased") then dmg = dmg / 2 end
	if self:GetCriticalHit() then dmg = dmg * 3 end
	return dmg
end

function SWEP:GetModDelay(del)
	local del = del or self.Primary.Delay
	if GAMEMODE:MSCheckForMod("Rate of Fire Increased") then del = del / 2 end
	if GAMEMODE:MSCheckForMod("Rate of Fire Decreased") then del = del * 2.4 end
	return del
end

function SWEP:GetModCone(con)
	local con = con or self.Primary.Cone
	if GAMEMODE:MSCheckForMod("Accuracy Increased") then con = con / 2 end
	if GAMEMODE:MSCheckForMod("Accuracy Decreased") then con = con * 4 end
	if self:GetNWBool("ScopeUsing", false) then 
		con = self.Primary.AimingCone or (con / 2)
		if con <= 0.01 then con = 0 end
	end
	return con
end

function SWEP:GetModSound()
	-- placeholder
	-- will later do stuff like "voice sounds", "cd-i sounds", etc
	return self.Primary.Sound
end

function SWEP:GetModTracer(trc)
	local trc = trc or self.Tracer or "Tracer"
	if self:GetCriticalHit() then return "supplasercrit" end
	if GAMEMODE:MSCheckForMod("Laser Tracers") then return "markingtracer" end
	return trc
end

function SWEP:DetermineCriticalHit()
	if GAMEMODE:MSCheckForMod("Critical Hits") and math.random(10)==1 then
		self:SetNWBool("CriticalHit",true)
	else
		self:SetNWBool("CriticalHit",false)
	end
end

function SWEP:GetCriticalHit()
	return self:GetNWBool("CriticalHit",false)
end

function SWEP:SetCriticalHit(crit)
	return self:SetNWBool("CriticalHit",crit)
end

function SWEP:MetastrikeCallback(attacker, tr, dmginfo)
	if tr.Entity:IsPlayer() or tr.Entity:IsNPC() then
		if GAMEMODE:MSCheckForMod("Guns Steal Health") and (tr.Entity:Team()!=attacker:Team()) and SERVER then
			healamt = dmginfo:GetDamage()
			self.Owner:SetHealth(math.min(self.Owner:Health()+healamt*(3/4), self.Owner:GetMaxHealth()))
		end
		if GAMEMODE:MSCheckForMod("Knockback Bullets") and tr.Entity:Team()~=self.Owner:Team() then
			tr.Entity:SetVelocity(self.Owner:GetForward()*500)
		end
		if missed and SERVER then
			tr.Entity:EmitSound("metastrike/misc/miss.wav")
			self:EmitSound("metastrike/misc/miss.wav")
			local effectdata = EffectData()
			effectdata:SetStart( tr.HitPos ) // not sure if we need a start and origin (endpoint) for this effect, but whatever
			effectdata:SetOrigin( tr.HitPos )
			--effectdata:SetAngles( tr.HitNormal )
			effectdata:SetNormal( tr.HitNormal )
			effectdata:SetScale( 10 )
			util.Effect( "StunstickImpact", effectdata )
		end
		if GAMEMODE:MSCheckForMod("Incendiary Ammo") and (tr.Entity:Team()!=attacker:Team()) and SERVER and !missed then
			tr.Entity.OnFireTime=CurTime()+10.5
			tr.Entity.OnFireAttacker=self.Owner
		end
		if self:GetCriticalHit() then
			tr.Entity:EmitSound( Sound( "weapons/stunstick/stunstick_fleshhit1.wav" ) )
		end
	end
end